
package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogincException;
import cn.wolfcode.wolf2w.interceptor.RequireLongin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.redisService.IStrategyStatisVOService;
import cn.wolfcode.wolf2w.redisService.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redisService.impl.StrategyStatisVOServiceImpl;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.RedisKeys;
import org.apache.el.util.JreCompat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.dc.pr.PRError;
import sun.plugin2.util.NativeLibLoader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
     private IStrategyStatisVOService strategyStatisVOService;

    /**
     * 根据id查数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Object get(String id){
        return  userInfoService.get(id);
    }

    /**
     * 查手机号是否存在
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
            boolean ret = userInfoService.checkPhone(phone);
            return !ret;
    }
    //发送短信
    @GetMapping("/sendVerifyCode")
    public Object sendVerifode(String phone){
        System.out.println(phone);
        userInfoService.sendVerifyCode(phone);
        //成功
        return JsonResult.success();
    }
    //注册跳到登录
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){
       /* try {

            userInfoService.rehist(phone,nickname,password,rpassword,verifyCode);
            //异常分为两周 1给用户看的用login 2给自己看的
        }catch (LogincException e){
            e.getStackTrace();
            return new JsonResult<>(JsonResult.CODE_ERROR_PARAM,e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.defaultError();
        }*/
        userInfoService.rehist(phone,nickname,password,rpassword,verifyCode);
        return JsonResult.success();
    }
    @PostMapping("/login")
    public Object login(String username,String password){
        //用户登录操作
        UserInfo userInfo = userInfoService.userLogin(username,password);

        //创建token
        String token= UUID.randomUUID().toString().replaceAll("-","");
        //使用token作为key, 用户对象做为vlaue存到redis中
        userInfoRedisService.setToken(token, userInfo);
        //将token值传到客户端保存起来
        Map<String, Object> map = new HashMap<>();
        map.put("token", RedisKeys.USER_LOGIN.join(token));
        map.put("user", userInfo);
        return JsonResult.success(map);
    }
    @RequireLongin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        //1.通过请求对象的getHeader("token")方法获取浏览器传入的token值
        String doken = request.getHeader("token");
        //2.通过token值 作为redis的key获取之前缓存在redis里面的user对象
        UserInfo userInfo = userInfoRedisService.getHeader(doken);
        System.out.println(userInfo);
        return JsonResult.success(userInfo);
    }

     @GetMapping("/user")
     public Object user( @UserParam UserInfo info){
        return  JsonResult.success(info);
     }

     //收藏小图标
     @GetMapping("/strategies/favor")
    public Object strategiesFavor(String sid,@UserParam  UserInfo userInfo){
         return JsonResult.success( strategyStatisVOService.strategiesFavor(sid,userInfo.getId()));
     }
}
